<?php
namespace Over\Controller;
use Think\Controller;
class IndexController extends Controller {
	public function index() {//2
		header('Content-Type:text/html; charset=utf-8');
		header('Refresh: 5');
    //$i=0;
	//list($s1,$s2)=explode(' ',microtime());
	//$s3= (float)sprintf('%.0f',(floatval($s1)+floatval($s2))*1000);
		$s2 = time();
		$StartTime = strtotime(date('Y-m-d H:i', time()));
	//$s4=($StartTime+35);//开奖开始时间
	//$s5=($StartTime+55);//开奖结束时间
		$Issue = ceil(($s2 - strtotime(date('Y-m-d', $s2))) / 60);
		$IssueNum = (date('Ymd', $s2) * 10000) + $Issue;
		echoo('>当前期号：' . $IssueNum);
///////////////////////////////////////////////新开始的1天数据
		if ($Issue == 1) {
			//echoo('初始化新数据');
			M('lotteryrecord')->where('StartTime<' . $StartTime - 180)->delete();
		}
		$mm = $s2 - $StartTime; //当前秒数
		if ($mm < 35) {
			$kaij = $StartTime - 29;
		} else {
			$kaij = $StartTime + 31;
		}
    //echoo (date('Y-m-d H:i:s',$StartTime));
    //echoo (date('Y-m-d H:i:s',$kaij));
  //////////////////////////////////////////////bet数据
		if ($mm < 30) {
			//echoo($IssueNum.'正在投注---'.$mm);
			addallsj($mm);
		}
  //////////////////////////////////////////////插入新的
		addisnum($IssueNum, $StartTime);
  //////////////////////////////////////////////开奖
		echoo('>正在开奖');
		$data2 = array('StartTime' => array('LT', $kaij), 'Status' => array('eq', 0),);
		$list = M('lotteryrecord')->where($data2)->select();
		if ($list) {//1
			echoo('>有新的开奖数据' . count($list) . '条');
			foreach ($list as $k => $v) {//2
				savekj($v['issuenum']);
			}
		} else {
			echoo('>没有有新的开奖数据');
		}
		echoo('>开奖结束');
  //////////////////////////////////////////////派奖
		echoo('>正在派奖');
		$map = array('CreateTime' => array('LT', $kaij), 'Id' => array('GT', 0), 'BetStatus' => 0,);
		$list = M('bet')->where($map)->group('IssueNum')->select();
		if ($list) {//1
			echoo('有' . count($list) . '期需要派奖');
			foreach ($list as $k => $v) {//2
				$list = M('lotteryrecord')->field('LotteryNum')->where(array('IssueNum' => $v['issuenum']))->find();
				if ($list && $list['lotterynum']) {
					if (!$list['lotterynum']) {
						savekj($v['issuenum']);
					} else {
						$countIssue = M('bet')->where(array('IssueNum' => $v['issuenum']))->count();
						echoo(($k + 1) . ' 第' . $v['issuenum'] . '期 开奖：' . $list['lotterynum'] . ' 有' . $countIssue . '条投注');
						for ($i = 0;$i < $countIssue;$i++) {
							$map['IssueNum'] = $v['issuenum'];
							$list2 = M('bet')->where($map)->find();
							//print_r($list2);
							if ($list['lotterynum'] == 1) {//1 正 2反
								$summonery = $list2['zmoney'] + ($list2['zmoney'] * 0.95) - ($list2['fmoney']);
								echoo('开奖号:' . $list2['kjhm'] . '1-正');
							} else {
								$summonery = $list2['fmoney'] + ($list2['fmoney'] * 0.95) - ($list2['zmoney']);
								echoo('开奖号:' . $list2['kjhm'] . '2-反');
							}
							echoo('盈亏：' . $summonery);
							M('user')->startTrans();
							$t = true;
							$result1 = M('bet')->where(array('Id' => $list2['id']))->data(array('BetStatus' => 2, 'winMoney' => $summonery, 'LotteryNum' => $list['lotterynum']))->save();
							if (!$result1) $t = flase;
							if ($summonery > 0 && $t) {//中奖了
								$data2 = array();
								$data2['type'] = 1;//2减少
								$data2['amount'] = $summonery;
								$data2['info'] = $list['issuenum'] . '奖金';
								$data2['time'] = $s2;
								$data2['uid'] = $list2['userid'];
								$result1 = M('monery')->data($data2)->add();
								if (!$result1) $t = flase;
								if ($t) {
									$result1 = M('user')->where(array('Id' => $list2['userid']))->setInc('monery', $summonery);
									if (!$result1) $t = flase;
								}
							}
							if ($t) {
								//echoo('更新成功');
								M('user')->commit();//成功则提交
							} else {
								//echoo('更新失败');
								M('user')->rollback();//不成功，则回滚
							}
						}
					}
				} else {
					//echoo($k.'> 第'.$v['issuenum'].'期无开奖数据,正在追加');
					addisnum($v['issuenum'], strtotime(date('Y-m-d H:i', $v['createtime'])));
				}
			}
		} else {
			echoo('没有需要派奖的数据');
		}
		echoo('派奖结束');
		unset($data2, $map, $list, $list);
	}
}